<?php
require_once($application_folder."/controllers/navigator.php");
class User extends Navigator{

    function __construct(){
        parent::__construct();
        
        /************************************************************
        * CHECK LOGIN
        *************************************************************/
        if(!$this->session->userdata('logged_in')){
            redirect(base_url());
        }
        
        $this->load->helper('date');
    }

    function index(){
        $this->preview();
    }

    function preview(){

        /************************************************************
        * GET User
        *************************************************************/
        $query = "SELECT user.*, typeofuser.description FROM user LEFT JOIN typeofuser ON user.typeofuser_id = typeofuser.id";

        $data['user'] = $this->db->query($query)->result_array();

        $this->core_cms('user', $data, 'preview');                                  
    }

    function newform(){

        $data = array();

        /************************************************************
        * GET user if Edit MODE
        *************************************************************/
        if(isset($_GET['id'])){
            $data['user'] = $this->db->get_where('user', array('id' => $_GET['id']))->result_array();    
        }

        $data['typeofuser'] = $this->db->get('typeofuser')->result_array();

        $this->core_cms('user', $data, 'new_edit_form');                                  
    }

    function edit_insert(){


        switch ($_POST['appmode']){

            case 'insert':

                if($this->validnewuserform()) { 
                    if($this->simple_login_secure->create()){
                        echo json_encode(array('action'=> true));
                    }else{
                        echo json_encode(array('action'=> false, 'msg' => 'Korisnik nije dodat u bazu.')); 
                    }
                }
                else{
                    echo json_encode(array('action'=> false, 'msg' => validation_errors())); 
                }

                break;

            case 'edit':

                $this->db->where('id', $_POST['id']);
                $this->db->update('user', array(
                'firstname' => $_POST['firstname'], 
                'lastname' => $_POST['lastname'], 
                'email' => $_POST['email'], 
                'typeofuser_id' => $_POST['typeofuser'], 
                'modified' => time()
                ));

                echo json_encode(array('action'=> true));

                break;

        }

    }

    function deleteone(){

        $this->db->where('id',$_POST['id']);
        $this->db->delete('user');
        echo json_encode(array('action'=> true));
    }

    function validnewuserform() {

        $this->form_validation->set_rules('firstname','<b>ime</b>','trim|xss_clean|required');        
        $this->form_validation->set_rules('lastname','<b>prezime</b>','trim|xss_clean|required');        
        $this->form_validation->set_rules('email','<b>email</b>','trim|xss_clean|required|valid_email');        
        $this->form_validation->set_rules('password_one','<b>lozinku</b>','trim|xss_clean|required');
        $this->form_validation->set_rules('password_two','<b>lozinku ponovo</b>','trim|xss_clean|required|matches[password_one]');

        $this->form_validation->set_message('required','Molimo unesite %s!'); 
        $this->form_validation->set_message('valid_email','Polje %s mora biti validan mejl!');
        $this->form_validation->set_message('matches','<b>Lozinke</b> moraju biti iste!');

        return $this->form_validation->run();

    }
    
    function reset_pass(){
        //Hash user_pass using phpass
        $user_pass_hashed = md5("123456");
        $this->db->where('id', $_POST['id']);
        $this->db->update('user', array('password' => $user_pass_hashed));
        echo json_encode(array('action'=> true));
        
    }

}